
package com.netease.xone.itemview;

import protocol.meta.Subject;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshExpandableListView;
import com.netease.image.ImageType;
import com.netease.xone.R;
import com.netease.xone.activity.ActivitySubjectDetail;
import com.netease.xone.widget.LoadingImageView;

public class RenderSubjectView implements PullToRefreshExpandableListView.ParentTypeTag{

    TextView subjectName;
    TextView hotCount;
    TextView subscribeCount;
    ViewGroup avatarClick;
    ViewGroup nameClick;
    LoadingImageView subjectAvatar;

    public RenderSubjectView(View v) {
        subjectName = (TextView)v.findViewById(R.id.subject_name);
        hotCount = (TextView)v.findViewById(R.id.subject_hot_count);
        subscribeCount = (TextView)v.findViewById(R.id.subject_subscribe_count);
        avatarClick = (ViewGroup)v.findViewById(R.id.subject_click_avatar);
        nameClick = (ViewGroup)v.findViewById(R.id.subject_click_name);
        subjectAvatar = (LoadingImageView)v.findViewById(R.id.subject_avatar);
        if(subjectAvatar != null){
            int size = v.getResources().getDimensionPixelSize(R.dimen.subject_avatar_dimen);
            subjectAvatar.setServerClipSize(size, size);
        }
    }

    public void renderView(final Context context, final Subject subject) {
        if (subjectName != null) {
            subjectName.setText(subject.subjectName);
        }
        if (hotCount != null) {
            hotCount.setText(String.valueOf(subject.subscribeCount));
        }
        if (subscribeCount != null) {
            int count = subject.sourceList == null ? 0 : subject.sourceList.size();
            subscribeCount.setText(context.getResources().getString(
                    R.string.subscribe_x_source_of_all, subject.mySubCount, count));
        }
        if (avatarClick != null && nameClick != null){
            OnClickListener click = new OnClickListener() {

                @Override
                public void onClick(View v) {
                    ActivitySubjectDetail.startActivity(context, subject.subjectId, subject.subjectName);
                }
            };
            avatarClick.setOnClickListener(click);
            nameClick.setOnClickListener(click);
        }
        if(subjectAvatar != null && !TextUtils.isEmpty(subject.avatar)){
            subjectAvatar.setImageResource(R.drawable.avatar_theme_default_circle);
            subjectAvatar.setLoadingImage(subject.avatar, ImageType.CircleMemCache);
        }

    }
}
